//
//  RootNewsViewController.m
//  CarWorld
//
//  Created by 曹杨杨 on 15/3/27.
//  Copyright (c) 2015年 CarWorldTeam. All rights reserved.
//

#import "RootNewsViewController.h"
#import "NewsViewController.h"
#import "PriceTableViewController.h"
#import "ConduitTableViewController.h"
#import "EudiometerTableViewController.h"
#import "FansTableViewController.h"

#import "HotSectionView.h"

@interface RootNewsViewController ()
@property (nonatomic, retain) HotSectionView *hotSectionView;
@property (nonatomic, retain) UIView *rootView;
@property (nonatomic, retain) UIView *tempView;
@property (nonatomic, retain) NewsViewController *NviewVC;
@end

@implementation RootNewsViewController

- (void)dealloc
{
    [_hotSectionView release];
    [_NviewVC release];
    [_rootView release];
    [_tempView release];
    [super dealloc];
}

- (void)changeView:(UIViewController *)VC
{
    [self.tempView removeFromSuperview];
    self.tempView = VC.view;
    [self.rootView addSubview:self.tempView];
    [self addChildViewController:VC];
}


- (void)clickedSectionView:(HotSectionView *)hotSectionView
{
    NSInteger index = hotSectionView.keyButton.tag;
    switch (index) {
        case 0://综述
        {
            [self changeView:_NviewVC];
        }
            break;
        case 1://图片
        {
            
            PriceTableViewController  *priceVC = [[PriceTableViewController alloc] init];
            priceVC.view.frame = self.tempView.frame;
            [self changeView:priceVC];
            [priceVC release];
        }
            break;
        case 2://资讯
        {
            ConduitTableViewController *conVC = [[ConduitTableViewController alloc] init];
            conVC.view.frame = self.tempView.frame;
            [self changeView:conVC];
            [conVC release];
        }
            break;
        case 3://活动
        {
            EudiometerTableViewController *eudVC = [[EudiometerTableViewController alloc] init];
            eudVC.view.frame = self.tempView.frame;
            [self changeView:eudVC];
            [eudVC release];
        }
            break;
        case 4://论坛
        {
            FansTableViewController *fanVC = [[FansTableViewController alloc] init];
            fanVC.view.frame = self.tempView.frame;
            [self changeView:fanVC];
            [fanVC release];
        }
            break;
        default:
            break;
    }
}



-(void)segmentAction:(UISegmentedControl *)Seg{
    
    NSInteger Index = Seg.selectedSegmentIndex;
    
    NSLog(@"Index %lu", Index);
    
    switch (Index) {
        case 0:
        {
            
            //1.移除self.subViews的所有元素
            //2.移除跟控制的self.childViewControllers的所有元素
            //3.[self addChildViewControllers];添加当前需要显示的视图控制器
            //4.[self.view addSubViews];添加当前需要现实的视图
//            [self.view removeFromSuperview];
//            [self.childViewControllers ]
//            NSArray *ar = [NSArray arrayWithObjects:@"1", nil];
//            [self addChildViewController:_newsVC];
//            [self.view addSubview:_newsVC.view];
            
            
            [self changeView:_NviewVC];
        }
            break;
        case 1:
        {
            PriceTableViewController  *priceVC = [[PriceTableViewController alloc] init];
            priceVC.view.frame = self.tempView.frame;
            [self changeView:priceVC];
            [priceVC release];
        }
            break;
        case 2:
        {
            ConduitTableViewController *conVC = [[ConduitTableViewController alloc] init];
            conVC.view.frame = self.tempView.frame;
           [self changeView:conVC];
            [conVC release];
        }
            break;
        case 3:
        {
            EudiometerTableViewController *eudVC = [[EudiometerTableViewController alloc] init];
            eudVC.view.frame = self.tempView.frame;
            [self changeView:eudVC];
            [eudVC release];

        }
            break;
        case 4:
        {
            FansTableViewController *fanVC = [[FansTableViewController alloc] init];
            fanVC.view.frame = self.tempView.frame;
           [self changeView:fanVC];
            [fanVC release];
        }
            break;
    }
}







- (void)viewDidLoad {
    [super viewDidLoad];
     self.navigationItem.title = @"最新时讯";
     self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:41/255.0 green:129/255.0 blue:255/255.0 alpha:1];
    NSArray *segmentedArray = [[NSArray alloc]initWithObjects:@"最新",@"行情",@"导购",@"测评",@"车迷",nil];
    //初始化UISegmentedControl
//    UISegmentedControl *segmentedControl = [[UISegmentedControl alloc]initWithItems:segmentedArray];
    
    CGRect rect = [UIScreen mainScreen].bounds;
    CGFloat width = rect.size.width;
    CGFloat height = rect.size.height * 0.055;
    _hotSectionView = [[HotSectionView alloc] initWithFrame:CGRectMake(0, 64, width, height)];
    [self.view addSubview:_hotSectionView];
    
    NSMutableArray *arr = [NSMutableArray arrayWithCapacity:5];
    for (int i = 0; i < segmentedArray.count; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
        [btn setTitle:segmentedArray[i] forState:UIControlStateNormal];
        [arr addObject:btn];
    }
    self.hotSectionView.buttons = arr;
    self.hotSectionView.delegate = self;
//    segmentedControl.frame = CGRectMake(0, 64, width, height);
//    segmentedControl.selectedSegmentIndex = 0;//设置默认选择项索引
//    [segmentedControl addTarget:self action:@selector(segmentAction:)forControlEvents:UIControlEventValueChanged];  //添加委托方法
//    [self.view addSubview:segmentedControl];
//    [segmentedArray release];
//    [segmentedControl release];
    CGFloat rheight = rect.size.height * 0.945;
    CGFloat topHeight = height + 64;
    _rootView = [[UIView alloc] initWithFrame:CGRectMake(0, topHeight, width, rheight)];
    _rootView.backgroundColor = [UIColor greenColor];
    [self.view addSubview:_rootView];
    
    self.tempView = nil;
    
    NewsViewController *newVC = [[NewsViewController alloc] init];
    [self changeView:newVC];
    self.NviewVC = newVC;
    [newVC release];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
